# This takes "only one" MetaWidget object, which has no connection with
# real widget object, and then creates real widget object, link it to
# MetaWidget object.

import wx

# TODO: Modify all imports to Lazy-imports
from wxpyview.ui.component.widgets.button.basic.Button import *
from wxpyview.ui.component.widgets.button.checkbox.CheckBox import *
from wxpyview.ui.component.widgets.button.radio.RadioButton import *
from wxpyview.ui.component.widgets.button.radio.RadioButtonGroup import *
from wxpyview.ui.component.widgets.choice.Choice import *
from wxpyview.ui.component.widgets.control.inputbox.TextCtrl import *
from wxpyview.ui.component.widgets.control.spin.SpinCtrl import *
from wxpyview.ui.component.widgets.control.tree.TreeCtrl import *
from wxpyview.ui.component.widgets.dialog.Dialog import *
from wxpyview.ui.component.widgets.dialog.builtin.ColorDialog import *
from wxpyview.ui.component.widgets.dialog.builtin.FileDialog import *
from wxpyview.ui.component.widgets.dialog.builtin.FontDialog import *
from wxpyview.ui.component.widgets.dialog.builtin.MessageDialog import *
from wxpyview.ui.component.widgets.frame.basic.Frame import *
from wxpyview.ui.component.widgets.frame.mdi.MDIParentFrame import *
from wxpyview.ui.component.widgets.frame.mdi.MDIChildFrame import *
from wxpyview.ui.component.widgets.image.Image import *
from wxpyview.ui.component.widgets.listbox.ListBox import *
from wxpyview.ui.component.widgets.menu.Menu import *
from wxpyview.ui.component.widgets.menu.MenuBar import *
from wxpyview.ui.component.widgets.menu.MenuItem import *
from wxpyview.ui.component.widgets.menu.MenuSeparator import *
from wxpyview.ui.component.widgets.notebook.Tab import *
from wxpyview.ui.component.widgets.notebook.TabGroup import *
from wxpyview.ui.component.widgets.panel.Panel import *
from wxpyview.ui.component.widgets.sizer.box.BoxItem import *
from wxpyview.ui.component.widgets.sizer.box.HBox import *
from wxpyview.ui.component.widgets.sizer.box.VBox import *
from wxpyview.ui.component.widgets.sizer.flexgrid.FlexGridCell import *
from wxpyview.ui.component.widgets.sizer.flexgrid.FlexGridRow import *
from wxpyview.ui.component.widgets.sizer.flexgrid.FlexGridSizer import *
from wxpyview.ui.component.widgets.slider.HSlider import *
from wxpyview.ui.component.widgets.slider.VSlider import *
from wxpyview.ui.component.widgets.staticline.HLine import *
from wxpyview.ui.component.widgets.staticline.VLine import *
from wxpyview.ui.component.widgets.statictext.StaticText import *
from wxpyview.ui.component.widgets.statusbar.StatusBar import *

class WidgetGenerator:
    def __init__(self):
        self.class_map = {"wx:BoxItem"           : BoxItem,
                          "wx:Button"            : Button,
                          "wx:CheckBox"          : CheckBox,
                          "wx:Choice"            : Choice,                         
                          "wx:ColorDialog"       : ColorDialog,
                          "wx:Dialog"            : Dialog,                          
                          "wx:FileDialog"        : FileDialog,
                          "wx:FlexGrid"          : FlexGridSizer,
                          "wx:FlexGridRow"       : FlexGridRow,
                          "wx:FlexGridCell"      : FlexGridCell,
                          "wx:FontDialog"        : FontDialog,
                          "wx:Frame"             : Frame,
                          "wx:HBox"              : HBox,
                          "wx:HLine"             : HLine,
                          "wx:HSlider"           : HSlider,
                          "wx:Image"             : Image,                      
                          "wx:Panel"             : Panel,
                          "wx:SpinCtrl"          : SpinCtrl,
                          "wx:StaticText"        : StaticText,
                          "wx:TextCtrl"          : TextCtrl,
                          "wx:TreeCtrl"          : TreeCtrl,                          
                          "wx:VSlider"           : VSlider,
                          "wx:ListBox"           : ListBox,
                          "wx:MDIChildFrame"     : MDIChildFrame,
                          "wx:MDIParentFrame"    : MDIParentFrame,                          
                          "wx:Menu"              : Menu,
                          "wx:MenuBar"           : MenuBar,                          
                          "wx:MenuItem"          : MenuItem,
                          "wx:MenuSeparator"     : MenuSeparator,
                          "wx:MessageDialog"     : MessageDialog,
                          "wx:RadioButton"       : RadioButton,
                          "wx:RadioButtonGroup"  : RadioButtonGroup,
                          "wx:StatusBar"         : StatusBar,
                          "wx:TabGroup"          : TabGroup,
                          "wx:Tab"               : Tab,
                          "wx:VBox"              : VBox,
                          "wx:VLine"             : VLine,
                     }
    
    def generate(self, meta_widget):
        wxrc_dom_object = meta_widget.getDOM() 

        wxrc_dom_node_name = wxrc_dom_object.nodeName

        klass = self.class_map.get(wxrc_dom_node_name, None)

        if klass != None:
            return self._wx_Widget(meta_widget, klass)

    def finalize(self, meta_widget):
        wxrc_dom_object = meta_widget.getDOM() 
        wxrc_dom_node_name = wxrc_dom_object.nodeName
        
        #if wxrc_dom_node_name not in ["wx:VBox", "wx:HBox", "wx:FlexGrid"]:

        klass = self.class_map.get(wxrc_dom_node_name, None)
        
        if klass != None:
            return self._wx_finalize(meta_widget, klass)

    # TODO: register reference of top-most windows such as Frame, Dialog, and so on.

    def _wx_Widget(self, meta_widget, klass):
        obj = klass().create(meta_widget)
        meta_widget.setWidget(obj)
        return obj
    
    def _wx_finalize(self, meta_widget, klass):
        if klass.__dict__.get('finalize', None) != None:
            klass.finalize(meta_widget)